package com.originskyseed.platform.cms.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.ComplainAndSuggestRelForward;
import com.originskyseed.platform.cms.service.IComplainAndSuggestRelForwardService;
import com.originskyseed.platform.cms.vo.request.ComplainAndSuggestRelForwardRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 投诉与建议转发表Controller类（OSI_COMPLAIN_AND_SUGGEST_REL_FORWARD）
 * 
 * @author ling
 */
@Controller
@RequestMapping("/complainAndSuggestRelForward")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class ComplainAndSuggestRelForwardController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ComplainAndSuggestRelForwardController.class);

	@DubboReference
	private IComplainAndSuggestRelForwardService complainAndSuggestRelForwardService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainAndSuggestRelForwardList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<ComplainAndSuggestRelForward>> complainAndSuggestRelForwardList(HttpServletRequest request) {
		BaseResponseBody<BasePage<ComplainAndSuggestRelForward>> result = null;
		try {
			// 请求对象
			ComplainAndSuggestRelForwardRequestVO requestVO = this.getParameterBean(ComplainAndSuggestRelForwardRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			// 分页查询
			BasePage<ComplainAndSuggestRelForward> page = new BasePage<ComplainAndSuggestRelForward>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.complainAndSuggestRelForwardService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<ComplainAndSuggestRelForward>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<ComplainAndSuggestRelForward>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainAndSuggestRelForwardSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<ComplainAndSuggestRelForward> complainAndSuggestRelForwardSave(HttpServletRequest request) {
		BaseResponseBody<ComplainAndSuggestRelForward> result = null;
		try {
			// 请求对象
			ComplainAndSuggestRelForward complainAndSuggestRelForward = this.getParameterBean(ComplainAndSuggestRelForward.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			if (StringUtils.isNotBlank(complainAndSuggestRelForward.getId())) {
				this.complainAndSuggestRelForwardService.updateByPrimaryKey(complainAndSuggestRelForward);
			} else {
				complainAndSuggestRelForward.setCreator(userInfo == null ? "" : userInfo.getName());
				complainAndSuggestRelForward.setCreatorId(userInfo == null ? "" : userInfo.getId());
				this.complainAndSuggestRelForwardService.insertSelective(complainAndSuggestRelForward);
			}
			result = new BaseResponseBody<ComplainAndSuggestRelForward>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), complainAndSuggestRelForward);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ComplainAndSuggestRelForward>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainAndSuggestRelForwardDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> complainAndSuggestRelForwardDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.complainAndSuggestRelForwardService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}